﻿using System;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using TransferPost.Beans;
using TransferPost.Docking;
using TransferPost.Forms;
using TransferPost.util;

/// <summary>
/// 2020年3月18日 重构 ZHZHH
/// </summary>
namespace TransferPost
{
    public partial class frmMain : Form
    {
        frmSVNCheck currentDockFrmMain;

        frmBottom dockFrmBottom;

        DockFrmLeft dockFrmLeft;

        public frmMain()
        {
            InitializeComponent();
        }


        private void frmMain_Load(object sender, EventArgs e)
        {
            dockFrmBottom = new frmBottom();
            dockFrmBottom.CloseButton = false;
            dockFrmBottom.Show(this.dockPanel1, TransferPost.Docking.DockState.DockBottom);
            dockFrmBottom.Text = "提示信息";
            dockFrmBottom.TabText = "TipMsg";

            //dockFrmLeft = new DockFrmLeft();
            //dockFrmLeft.CloseButton = false;
            //dockFrmLeft.Show(this.dockPanel1, TransferPost.Docking.DockState.DockLeft);
            //dockFrmLeft.Text = "";
            //dockFrmLeft.TabText = "空";

            //dockFrmLeft.AutoScroll = true;


            // TODO: 初始化全局Ctx;
            //PostContext.InitPrintEnv(delegate () { this.aaa(); });

            AppendText("友情提醒，所有操作的日志都在logs下可以查看...." + System.Environment.NewLine);

            this.Text += "V" + Application.ProductVersion.ToString();

            var version_Git = Tools.GetVersionInfo();
            var version_Local = new Version(Application.ProductVersion);

            if (version_Git > version_Local)
            {
                MessageBox.Show(string.Format("当前版本为{0},服务器有最新版本为{1}，请主动更新。", version_Local.ToString(), version_Git.ToString()));

                System.Diagnostics.Process.Start("https://gitee.com/friendm/TransferPost/releases");
            }
        }

        /// <summary>
        /// TODO：需要定制反射
        /// </summary>
        /// <param name="TabText"></param>
        /// <returns></returns>
        private DockContent FindDockContent(string TabText)
        {
            object content = dockPanel1.Contents.FirstOrDefault(x => x.DockHandler.TabText == TabText);

            if (content == null)
                return null;
            else
                return content as DockContent;
        }

        /// <summary>
        /// TODO：需要定制反射
        /// </summary>
        /// <param name="pro"></param>
        /// <returns></returns>
        private frmSVNCheck ActiveDockFrmMain(frmInfo pro)
        {
            DockContent newFrm = FindDockContent(pro.frmGuid);

            // TODO：需要反射
            if (newFrm == null)
            {
                //说明没有这个页面
                //newFrm = new frmSVNCheck(this);
                newFrm = Activator.CreateInstance(pro.frmType, new object[] { this }) as DockContent;
                newFrm.Text = pro.frmName;
                newFrm.Tag = pro;

                newFrm.TabText = pro.frmName;
            }

            newFrm.Show(this.dockPanel1);

            newFrm.BringToFront();

            return newFrm as frmSVNCheck;
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("啊哈");
        }

        #region " 输出提示信息 "

        protected delegate void AppendTextCallback(string text);

        public void AppendText(string text)
        {
            if (this.dockFrmBottom.InvokeRequired)
            {
                AppendTextCallback d = new AppendTextCallback(AppendText);
                this.dockFrmBottom.Invoke(d, text);
            }
            else
            {

                this.dockFrmBottom.AppendText(text);
                Application.DoEvents();
            }
        }
        #endregion

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Application.Exit();
            System.Environment.Exit(0);
        }

        private void tgb_Click(object sender, EventArgs e)
        {
            string strTag = ((ToolStripButton)sender).Tag.ToString();

            frmInfo info = PostContext.Instance.FormList.First(x => x.frmNo.Equals(strTag));

            currentDockFrmMain = ActiveDockFrmMain(info);
        }
    }
}
